#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int n = gas.size();
        int start = 0, end = 0;
        while (start < n)
        {
            int i = 0;
            int totalCost = 0;
            while (i < n)
            {
                int cur = (i + start) % n;
                totalCost += gas[cur];
                totalCost -= cost[cur];
                if (totalCost < 0) break;
                ++i;
            }
            if (i == n) return start;
            else start = i + start + 1;
        }
        return -1;
    }
};